package com.woniu.server;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @author jiang
 */
@Component
public class RedisUtil {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    @Resource
    private RedisTemplate<String, Object> redisObjectTemplate;
    public String get(String key) {
        return stringRedisTemplate.opsForValue().get(key);
    }
    public void set(String key,String value){
        stringRedisTemplate.opsForValue().set(key,value);
    }
    // 设置过期时间,秒
    public void set(String key,String value,long timeout){
        stringRedisTemplate.opsForValue().set(key,value,timeout, TimeUnit.SECONDS);
    }
    public void del(String key){
        stringRedisTemplate.delete(key);
    }
    public long ttl(String key){
        return stringRedisTemplate.getExpire(key, TimeUnit.SECONDS);
    }
    public boolean exists(String key){
        return stringRedisTemplate.hasKey(key);
    }
    public void expire(String key,long timeout){
        stringRedisTemplate.expire(key, timeout, TimeUnit.SECONDS);
    }
    public void increment(String key){
        stringRedisTemplate.opsForValue().increment(key);
    }
    public void sadd(String key, Set<String> set){
        String[] strings = new String[set.size()];
        stringRedisTemplate.opsForSet().add(key,set.toArray(strings));
    }
    public Set<String> smembers(String key){
        return stringRedisTemplate.opsForSet().members(key);
    }
    public boolean sismember(String key,String member){
        return stringRedisTemplate.opsForSet().isMember(key,member);
    }
    public Double score(String key,String value){
        return stringRedisTemplate.opsForZSet().score(key,value);
    }
    public void zadd(String key, String member, long score){
        stringRedisTemplate.opsForZSet().add(key,member,score);
    }
    public void zincrby(String key, String member, long score){
        stringRedisTemplate.opsForZSet().incrementScore(key,member,score);
    }
    public Set<String> zrerange(String key,long start,long end){
        return stringRedisTemplate.opsForZSet().reverseRange(key,start,end);
    }
    public long zcard(String key){
        return stringRedisTemplate.opsForZSet().size(key);
    }
    public void zrem(String key, String member){
        stringRedisTemplate.opsForZSet().remove(key,member);
    }
    public void saveCache(String key,Object value){
        redisTemplate.opsForValue().set(key,value);
    }
    public void saveCache(String key,Object value , long expire){
        redisTemplate.opsForValue().set(key,value,expire,TimeUnit.SECONDS);
    }
    public Object getCacheObject(String key){
        return redisTemplate.opsForValue().get(key);
    }

    public void saveHash(String key,Map<Object,Object> map){
        stringRedisTemplate.opsForHash().putAll(key,map);
    }
    public void saveHash(String key, String hashKey, String hashValue){
        stringRedisTemplate.opsForHash().put(key,hashKey,hashValue);
    }
    public void hincrBy(String key, String hashKey, long number){
        stringRedisTemplate.opsForHash().increment(key,hashKey,number);
    }
    public String hget(String key, String hashKey){
        return stringRedisTemplate.opsForHash().get(key,hashKey).toString();
    }
    public Map<Object,Object> hgetAll(String key){
        return stringRedisTemplate.opsForHash().entries(key);
    }
    public boolean hexists(String key, String hashKey){
        return stringRedisTemplate.opsForHash().hasKey(key,hashKey);
    }

    public Long getLong(String key){
        return stringRedisTemplate.opsForValue().get(key) != null ? Long.parseLong(stringRedisTemplate.opsForValue().get(key)) : 0L;
    }
    //


    /**
     * 读取数据
     */
    public String getString(final String key) {
        return stringRedisTemplate.opsForValue().get(key);
    }

    /**
     * 写入数据
     */
    public void setString(final String key, String value) {
        stringRedisTemplate.opsForValue().set(key, value);
    }
    /**
     * 写入数据并设置有效时间
     */
    public void setString(final String key, String value,long seconds) {
        stringRedisTemplate.opsForValue().set(key, value,seconds, TimeUnit.SECONDS);
    }
    /**
     * 删除数据
     */
    public void delete(final String key) {
        stringRedisTemplate.delete(key);
    }
    /**
     * 判断key是否存在
     */
    public boolean hasKey(final String key) {
        return stringRedisTemplate.hasKey(key);
    }
    /**
     * 自增
     */
    public long incr(final String key) {
        return stringRedisTemplate.opsForValue().increment(key);
    }
    /**
     * 查看键剩余时间
     */
    public long getExpire(final String key) {
        return stringRedisTemplate.getExpire(key);
    }
    /**
     * 向set集合内添加数据
     */
    public void addSet(String key, List<String> values) {
        String[] array = new String[values.size()];
        stringRedisTemplate.opsForSet().add(key,values.toArray(array));
    }
    /**
     * 向set集合内添加一条数据
     */
    public void addSet(String key, String value) {
        stringRedisTemplate.opsForSet().add(key, value);
    }
    /**
     * 移除Zset集合中指定元素
     */
    public void removeSet(String key, String value) {
        stringRedisTemplate.opsForSet().remove(key, value);
    }
    /**
     * 获取set集合内所有数据
     */
    public Set<String> getSet(String key) {
        return stringRedisTemplate.opsForSet().members(key);
    }
    /**
     * 向Zset集合中添加数据
     */
    public void addZset(String key, String value, long score) {
        stringRedisTemplate.opsForZSet().add(key, value, score);
    }
    /**
     * 获取Zset集合内所有数据
     */
    public Set<String> getZset(String key) {
        return stringRedisTemplate.opsForZSet().range(key, 0, -1);
    }
    /**
     * 查看Zset集合中指定键的排序值(判断该键是否存在)
     */
    public Double getScore(String key, String value) {
        return stringRedisTemplate.opsForZSet().score(key, value);
    }
    /**
     * 设置Zset集合中指定键的排序值
     */
    public void setScore(String key, String value, long score) {
        stringRedisTemplate.opsForZSet().add(key, value, score);
    }
    /**
     * 使Zset集合中指定键的排序值自增或自减
     */
    public void incrScore(String key, String value, long delta) {
        stringRedisTemplate.opsForZSet().incrementScore(key, value, delta);
    }
    /**
     * Zset集合分页查询
     */
    public Set<String> getZsetPage(String key, long start, long end) {
        return stringRedisTemplate.opsForZSet().reverseRange(key, start, end);
    }
    /**
     * 查询Zset集合中元素个数
     */
    public long getZsetSize(String key) {
        return stringRedisTemplate.opsForZSet().size(key);
    }
    /**
     * 移除Zset集合中指定元素
     */
    public void removeZset(String key, String value) {
        stringRedisTemplate.opsForZSet().remove(key, value);
    }
    /**
     * 传入任意类型，自动转换成string存储至数据库
     */
    public void setObject(String key, Object object) {
        redisObjectTemplate.opsForValue().set(key, object);
    }
    /**
     * 从数据库取出数据，并自动转换成指定类型
     */
    public Object getObject(String key) {
        return redisObjectTemplate.opsForValue().get(key);
    }
    /**
     * 传入任意类型，自动转换成string存储至数据库，并设定有效时间
     */
    public void setObject(String key, Object object, long seconds) {
        redisObjectTemplate.opsForValue().set(key, object, seconds, TimeUnit.SECONDS);
    }
    /**
     * 改变Hash中值的数值
     */
    public void incrHash(String key, String hashKey, long delta) {
        stringRedisTemplate.opsForHash().increment(key, hashKey, delta);
    }
    /**
     * 添加Hash键值对
     */
    public void addHash(String key, Map<Object,Object> map) {
        stringRedisTemplate.opsForHash().putAll(key, map);
    }
    /**
     * 获取HashMap中某一个键的值
     */
    public String getHash(String key, String hashKey) {
        return stringRedisTemplate.opsForHash().get(key, hashKey).toString();
    }
    /**
     * 获取HashMap中所有键值对
     */
    public Map<Object, Object> getAllHash(String key) {
        return stringRedisTemplate.opsForHash().entries(key);
    }
    /**
     * 删除Hash键值对
     */
    public void removeHash(String key, String hashKey) {
        stringRedisTemplate.opsForHash().delete(key, hashKey);
    }
    /**
     * 向Hash中添加一条数据
     */
    public void addHash(String key, String hashKey, String value) {
        stringRedisTemplate.opsForHash().put(key, hashKey, value);
    }
}
